package org.skipper.samples.redission.string;

import org.redisson.api.RedissonClient;
import org.skipper.samples.redission.enums.RedisKeyEnum;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author shoujian
 * Date: 2021-08-18
 * Time: 下午11:08
 */
@Component
public class StringSource {

    @Qualifier(value = "redis-cloud_0")
    @Resource
    private RedissonClient redissonClient;


    public void get() {
        String get_get = (String) redissonClient.getBucket(RedisKeyEnum.STRING_TEST.getCode()).get();
        System.out.println("get_get = " + get_get);
    }

    public void getset() {
        String getset_getAndSet = (String) redissonClient.getBucket(RedisKeyEnum.STRING_TEST.getCode()).getAndSet("test_getset");
        System.out.println("getset_getAndSet = " + getset_getAndSet);
        String getset_getAndSet_expire = (String) redissonClient.getBucket(RedisKeyEnum.STRING_TEST.getCode()).getAndSet("test_getset_expire", 1, TimeUnit.SECONDS);
        System.out.println("getset_getAndSet_expire = " + getset_getAndSet_expire);
    }

    public void incr() {
        long incr_incrementAndGet = redissonClient.getAtomicLong(RedisKeyEnum.STRING_TEST.getCode()).incrementAndGet();
        System.out.println("incr_incrementAndGet = " + incr_incrementAndGet);
    }

    public void incrby() {
        redissonClient.getAtomicLong(RedisKeyEnum.STRING_TEST.getCode()).addAndGet(10);
    }

    public void set() {
        redissonClient.getBucket(RedisKeyEnum.STRING_TEST.getCode()).set("test");
    }

    public void setex() {
        redissonClient.getBucket(RedisKeyEnum.STRING_TEST.getCode()).set("test", 1, TimeUnit.DAYS);
    }

    public void setnx() {
        redissonClient.getBucket(RedisKeyEnum.STRING_TEST.getCode()).trySet("test");
    }

}
